package io.github.yotta.logger.example.controller;

import io.github.yotta.logger.anno.annotation.LogModel;
import io.github.yotta.logger.anno.annotation.LogOperation;
import io.github.yotta.logger.anno.annotation.context.BizIdLogContext;
import io.github.yotta.logger.anno.annotation.provider.BizIdFrom;
import io.github.yotta.logger.example.param.Result;
import io.github.yotta.logger.example.param.UserCreateReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * @author yotta
 * @date 2023/3/22 14:57
 */
@RestController
@Api("日志测试")
@RequestMapping("/api/example")
@LogModel("用户登录")
public class LoggerExampleController {


    /**
     * 测试用例1：
     * 1. 使用LogOperation标注这是一个切入点
     * 2. 由于LogOperation注解没有添加任何信息，将从方法中的swagger注解中获取日志信息
     */
    @LogOperation
    @ApiOperation("测试方法1")
    @GetMapping("/test1")
    public Long test1() {
        return Long.MAX_VALUE;
    }

    /**
     * 测试用例2：
     * 1. 接口上添加LogOperation注解，
     * 2. 由于LogOperation注解中标注当前日志操作的信息，优先获取LogOperation注解
     */
    @LogOperation(model = "测试模块:LogOperation", value = "测试方法2:LogOperation")
    @ApiOperation("测试方法2")
    @GetMapping("/test2")
    public String test2() {
        System.out.println("1111");
        return "HelloWorld-2";
    }

    /**
     * 测试用例3，没有LogOperation，不会记录日志
     */
    @ApiOperation("测试方法3")
    @GetMapping("/test3/{id}/{name}")
    public String test3(@PathVariable(name = "id") Long id, @PathVariable(name = "name") String name) {
        return "HelloWorld-3";
    }

    /**
     * 测试用例4，方法有异常，但是设置了不记录错误，所以不记录日志
     */
    @ApiOperation(value = "测试方法4")
    @GetMapping("/test4/{id}/{name}")
    @LogOperation(logError = false)
    public String test4(@PathVariable(name = "id") Long id, @PathVariable(name = "name") String name) {
        throw new IllegalArgumentException("参数不合法");
    }

    /**
     * 测试用例5：测试记录业务id，从参数获取
     *
     * @return
     */
    @ApiOperation(" 测试用例5：测试记录业务id，从参数获取")
    @GetMapping("/test5")
    @LogOperation(bizIdPath = "req.id", bizIdFrom = BizIdFrom.PARAM)
    public String test5(UserCreateReq req) {
        return "HelloWorld-5";
    }

    /**
     * 测试用例6：测试记录业务id，从参数获取
     *
     * @return
     */
    @ApiOperation(" 测试用例5：测试记录业务id，从参数获取")
    @GetMapping("/test6")
    @LogOperation(bizIdPath = "id", bizIdFrom = BizIdFrom.PARAM)
    public String test5(String id) {
        return "HelloWorld-5";
    }


    /**
     * 测试用例7：测试记录业务id,从返回值获取
     *
     * @return
     */
    @ApiOperation("测试用例7：测试记录业务id,从返回值获取")
    @GetMapping("/test7")
    @LogOperation(bizIdPath = "id", bizIdFrom = BizIdFrom.RETURN)
    public UserCreateReq test6(UserCreateReq req) {

        UserCreateReq userCreateReq = new UserCreateReq();
        userCreateReq.setId("123");
        return userCreateReq;
    }

    /**
     * 测试用例7：测试记录业务id,从复杂返回值获取
     *
     * @return
     */
    @ApiOperation("测试用例8：测试记录业务id,从复杂返回值获取")
    @GetMapping("/test8")
    @LogOperation(bizIdPath = "data.id", bizIdFrom = BizIdFrom.RETURN)
    public Result<UserCreateReq> test7() {
        UserCreateReq userCreateReq = new UserCreateReq();
        userCreateReq.setId("123");
        return Result.success(userCreateReq);
    }

    /**
     * 测试用例7：测试记录业务id,从复杂返回值获取
     *
     * @return
     */
    @ApiOperation("测试用例9：测试记录业务id,从context中获取")
    @GetMapping("/test9")
    @LogOperation()
    public Result<Boolean> test9() {
        UserCreateReq userCreateReq = new UserCreateReq();
        userCreateReq.setId("123");
        BizIdLogContext.set("123");
        return Result.success(Boolean.getBoolean("true"));
    }

    /**
     * 测试用例5：测试记录业务id，从参数获取
     *
     * @return
     */
    @ApiOperation(" 测试用例10：测试记录业务id，从参数获取")
    @PostMapping("/test10")
    @LogOperation(bizIdPath = "req.req.id", bizIdFrom = BizIdFrom.PARAM)
    public String test10(@RequestBody UserCreateReq req) {
        return "HelloWorld-5";
    }
}
